<?php

/**
 *
 * @author ben <ben@spicatech.cn>  Created on 2015-6-27
 */
class ProjectController extends CController {

    public $toolbarTitle = '';
    public $pageTitle = '';
    public $host = '';
    public $hostid = 0;

    /**
     * 
     */
    public function init() {
        Yii::app()->attachEventHandler('onException', array($this, 'exceptionEvent'));
        Yii::app()->attachEventHandler('onError', array($this, 'exceptionError'));
        $this->host = filter_input(INPUT_SERVER, 'HTTP_HOST');
        $arr = explode('.', $this->host);
        $this->hostid = $arr[0];
    }

    /**
     * 
     * @param CEvent $event
     */
    protected function exceptionEvent(CEvent $event) {
        $this->handleException($event->exception);
    }

    /**
     * 
     * @param CErrorEvent $e
     */
    protected function exceptionError(CErrorEvent $e) {
        $msg = $e->message . ' on line ' . $e->line . ' on file ' . $e->file;
        $exception = new Exception($msg, $e->code);
        $this->handleException($exception);
    }

    /**
     * 
     * @param Exception $ex
     */
    protected function handleException(Exception $ex) {
        $this->error($ex->getMessage(), $ex->getCode());
        Yii::app()->end();
    }

    /**
     * 
     * @param type $msg
     * @param stdClass $data
     * @param type $code
     */
    protected function resultJson($msg, stdClass $data = null, $code = 0) {
        $callback = filter_input(INPUT_GET, 'callback');
        if (is_null($data)) {
            $data = new stdClass();
        }
        $output = array(
            'data' => $data,
            'msg' => $msg,
            'code' => intval($code)
        );
        $text = json_encode($output);
        if ($callback) {
            header('Content-type: text/javascript');
            $text = ';try {' . $callback . '(' . $text . ');} catch(e){};';
        } else {
            header('Content-type: application/json');
        }
        echo $text;
        Yii::app()->end();
    }

    /**
     * 
     * @param type $msg
     * @param stdClass $data
     * @param type $code
     */
    public function errorPage($msg, stdClass $data = null, $code = -1) {
        $this->layout = '//layouts/clean';
        $this->render('//result/error', array(
            'data' => $data,
            'code' => $code,
            'msg' => $msg
        ));
        Yii::app()->end();
    }

    /**
     * 
     * @param type $msg
     * @param stdClass $data
     */
    public function successPage($msg, stdClass $data = null) {
        $this->layout = '//layouts/clean';
        $this->render('//result/success', array(
            'data' => $data,
            'msg' => $msg
        ));
        Yii::app()->end();
    }

    /**
     * 
     * @param type $msg
     * @param stdClass $data
     */
    public function success($msg, stdClass $data = null) {
        if (Yii::app()->request->isAjaxRequest) {
            $this->resultJson($msg, $data, 0);
        } else {
            $this->successPage($msg, $data, 0);
        }
    }

    /**
     * 
     * @param type $msg
     * @param type $code
     * @param stdClass $data
     */
    public function error($msg, $code = -1, stdClass $data = null) {
        if ($code === 0) {
            $code = -1;
        }
        if (Yii::app()->request->isAjaxRequest) {
            $this->resultJson($msg, $data, $code);
        } else {
            $this->errorPage($msg, $data, $code);
        }
    }

    /**
     * 
     */
    public function getCurrentUrl() {
        $url = Yii::app()->request->getHostInfo() . Yii::app()->request->getUrl();
        return str_replace('index.php/', '', $url);
    }

}
